import leancloud
import sys
sys.path.append("..")
import Cust.leancloudapi
Getapi = Cust.leancloudapi.GetApi()
class  RollCall:
    def getAllLesson(self,id):
        query = leancloud.Query('Lesson')
        teacher = Getapi.getTeacher()
        todo_teacher = teacher.create_without_data(id)
        res = query.equal_to('TeacherInfo',todo_teacher).find()
        lesson_list = []
        for foo in res:
            lesson = {'id':foo.id,'name':foo.get('LessonName'),'term':foo.get('Term'),'grade':foo.get('Grade'),'academy':foo.get('Academy')}
            lesson_list.append(lesson)
        return lesson_list

    def getRollCall(self,id):
        query = leancloud.Query('RollCall')
        lesson = Getapi.getLesson()
        todo_lesson = lesson.create_without_data(id)
        res = query.equal_to('Lesson',todo_lesson).find()
        rollcall_list = []
        for foo in res:
            status = foo.get('Status')
            if foo.get('Status')==1:
                               
                rollcallinfo = {'id':foo.id,'date':foo.updated_at}
                rollcall_list.append(rollcallinfo)
        return rollcall_list

    def getRollCallInfo(self,id):
         query = leancloud.Query('RollCallInfo')
         rollcall = Getapi.getRollCall()
         todo_rollcall = rollcall.create_without_data(id)
         query.include('StudentInfo')
         res = query.equal_to('RollCall',todo_rollcall).find()
         rollcall_list = []
         for foo in res:
             status = foo.get('Status')
             student = foo.get('StudentInfo')
             rollcallinfo = {'no':student.get('StudentNo'),'name':student.get('StudentName'),'status':status}
             rollcall_list.append(rollcallinfo)
             
         return rollcall_list